//
//  ZYQVcardViewController.m
//  小庆聊天室
//
//  Created by 张毓庆 on 13-12-8.
//  Copyright (c) 2013年 张毓庆. All rights reserved.
//

#import "ZYQVcardViewController.h"
#import "ZYQAppDelegate.h"
#import "XMPPvCardTemp.h"
#import "ZYQLoginUser.h"
#import "ZYQEditVCardViewController.h"
#import "NSString+Helper.h"

@interface ZYQVcardViewController ()<ZYQEditVCardViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
{
    NSArray *_titlesArray; // 标题的数组
    NSArray *_lablesArray; // 标签控件的数组
}
- (IBAction)userLogout:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nickNameText;
@property (weak, nonatomic) IBOutlet UILabel *orgNameText;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberText;
@property (weak, nonatomic) IBOutlet UILabel *emailText;
@property (weak, nonatomic) IBOutlet UILabel *jidText;

@end

@implementation ZYQVcardViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	_titlesArray = @[@[@"头像",@"名称",@"jid"],@[@"公司",@"电话",@"邮箱"]];
    _lablesArray = @[@[_jidText,_nickNameText,_jidText],@[_orgNameText,_phoneNumberText,_emailText]];
    [self setupvCard];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSIndexPath *)indexPath
{
    // 取出要跳转的视图控制器
    ZYQEditVCardViewController *edit = segue.destinationViewController;
    // 设置一些属性
    edit.contextTitle = _titlesArray[indexPath.section][indexPath.row];
    edit.contentLabel = _lablesArray[indexPath.section][indexPath.row];
    
    // 设置代理
    [edit setDelegate:self];
}

#pragma mark - 电子名片的设置方法
- (void)setupvCard
{
    XMPPvCardTemp *myCard = [[[self appDelegate] xmppvCardModule] myvCardTemp];
    
    if (myCard == nil)
    {
        NSLog(@"我没有电子名片");
        // 实例化一个名片
        myCard = [XMPPvCardTemp vCardTemp];
    }
    
    if (myCard.nickname ==nil)
    {
        myCard.nickname = [ZYQLoginUser sharedZYQLoginUser].userName;
    }
    
    if (myCard.orgName == nil)
    {
        myCard.orgName = @"张毓庆聊天室";
    }
    
    NSData *photoData = [[xmppDelegate xmppvCardAvatarModule] photoDataForJID:myCard.jid];
    if (photoData)
    {
        _headImageView.image = [UIImage imageWithData:photoData];
    }
    
    if (myCard.note == nil)
    {
        myCard.note = @"未设置电话";
    }
    
    if (myCard.mailer == nil)
    {
        myCard.mailer = @"未设置电子信箱";
    }
    
    // 设置 jid
    if (myCard.jid == nil)
    {
        myCard.jid = [XMPPJID jidWithString:[ZYQLoginUser sharedZYQLoginUser].myJIDName];
    }
    
    // 保存或者更新名片
    [[[self appDelegate] xmppvCardModule] updateMyvCardTemp:myCard];
    
    
    if (myCard.photo)
    {
        self.headImageView.image = [UIImage imageWithData:myCard.photo];
    }
    
    self.nickNameText.text = myCard.nickname;
    self.orgNameText.text = myCard.orgName;
    self.jidText.text = [myCard.jid full];
    self.phoneNumberText.text = myCard.note;
    self.emailText.text = myCard.mailer;
}

#pragma mark 保存电子名片的方法
- (void)savevCard
{
    // 获取电子名片
    XMPPvCardTemp *myCard = [[[self appDelegate] xmppvCardModule] myvCardTemp];
    
    // 设置电子名片的内容
    myCard.photo = UIImagePNGRepresentation(_headImageView.image);
    myCard.nickname = _nickNameText.text;
    myCard.orgName = _orgNameText.text;
    myCard.note = _phoneNumberText.text;
    myCard.mailer = _emailText.text;
    
    // 保存电子名片
    [[[self appDelegate] xmppvCardModule] updateMyvCardTemp:myCard];
}

#pragma mark - AppDelegate 的助手方法
- (ZYQAppDelegate *)appDelegate
{
    return [[UIApplication sharedApplication] delegate];
}

#pragma mark - 注销登录
- (IBAction)userLogout:(id)sender
{
    [[self appDelegate] logout];
    
}

#pragma mark -  tableView的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell  = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.tag == 1)
    {
        [self performSegueWithIdentifier:@"EditVcardInfo" sender:indexPath];
    }
    else if (cell.tag == 2)
    {
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"照片库", nil];
        [sheet showInView:self.view];
    }
}

#pragma mark - 代理方法
- (void)editvCardViewControllerDidFinish
{
    [self savevCard];
}
#pragma mark - actionSheet的代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2)
    {
        return;
    }
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    
    // 设置照片源
    if (buttonIndex == 0)
    {
        [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }
    else
    {
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    
    [picker setAllowsEditing:YES];
    
    [picker setDelegate:self];
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - imagePicker的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    _headImageView.image = image;
    
    [self savevCard];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
